package edu.indiana.cs.b534.torrent;

import edu.indiana.cs.b534.torrent.impl.PieceInfo;

import java.io.File;

/**
 * this class is responsible for storing and downloaded torrent data. There can be multiple implementations
 * of this
 */
public interface StorageManager {

    /**
     * This will store a downloaded block in to the torrent
     *
     * @param pieceIndex   - index of the piece which this block belongs to
     * @param pieceOffset
     * @param bytes        - buffered data
     * @param sourceOffset - this is the offset of which we should look in the bytes buffer passed in
     */
    public void storeBlock(int pieceIndex, int pieceOffset, byte[] bytes, int sourceOffset) throws TorrentException;

    /**
     * This will check whether the given piece is completed or not
     *
     * @param pieceIndex
     * @return
     */
    public boolean isPieceCompleted(int pieceIndex);

    /**
     * will return the piece of the given index.
     *
     * @param pieceIndex
     * @return
     */
    public byte[] getPiece(int pieceIndex);

    public PieceInfo getPieceInfo(int pieceIndex);

    /**
     * This enables us to check whether we have completed this
     *
     * @return
     */
    public boolean isDownloadCompleted();

    /**
     * Informs the storage manager that the download is completed. Storage manager must make sure
     * it has got all the pieces and save data to the destination folder. If some pieces are missing
     * this must throw an exception.
     *
     * @throws TorrentException - when storage manager is notified that the download is completed,
     * but it hasn't got all of the torrent file.
     */
    public void setDownloadCompleted() throws TorrentException;

    /**
     * This method will help to initialize the storage for the expected number of pieces
     *
     * @param pieceInfo
     * @param storeFoler - this is the folder the completed file will be saved
     */
    public void initialize(PieceInfo[] pieceInfo, File storeFoler,int fileLength) throws TorrentException;

    /**
     * This will load the given file to be seeded.
     * @param fileToSeed
     * @param pieceInfo
     */
    public void initAsSeed(File fileToSeed, PieceInfo[] pieceInfo,int fileLength) throws TorrentException;

    /**
     * This will retrieve the piece availability. If I don't have anything, null should be returned
     * @return
     */
    public boolean[] getPieceAvailability();
    
    
    public int getFileSize();

    // following information will provide the tracker some status about our upload and download status
    public int getTotalDownloadedBytes();
    public int getTotalUploadedBytes();
    public int getBytesLeftToBeDownloaded();
}
